function Element (tagName, props, children) {
  this.tagName = tagName
  this.props = props
  this.children = children
  if (props.key)  this.key = props.key
  // 统计子元素个数
  let count = 0
  children.forEach((child, index) => {
    if (child instanceof Element) {
      count += child.count
    } else {
      children[index] = '' + child
    }
  })
  this.count = count
}

Element.prototype.render = function () {
  let el = document.createElement(this.tagName)
  let props = this.props
  for (let propName in props) {
    let propValue = props[propName]
    el.setAttribute(propName, propValue)
  }
  let children = this.children || []
  children.forEach(child => {
    let childEl = (child instanceof Element)
        ? child.render()
        : document.createTextNode(child)
    el.appendChild(childEl)
  })
  return el
}

function createElement (tagName, props, children) {
  return new Element(tagName, props, children)
}

export { createElement }
// module.exports = createElement
